<?php
!defined('P_W') && exit('Forbidden');

class PW_House_ExpiredDB extends BaseDB {
	var $_tableName = "pw_house_expired";
	var $_primaryKey = 'id';
	/**
	 * 
	 * 获取一条记录根据houseid type
	 * @param unknown_type $houseid
	 * @param unknown_type $type
	 */
	function get($houseid, $type){
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid = ' . pwEscape($houseid) . ' AND datatype = ' . pwEscape($type);
		return $this->_db->get_one($_sql);
	}
	/**
	 * 
	 * 获取多条记录根据houseid type
	 * @param unknown_type $houseid
	 * @param unknown_type $type
	 */
	function gets($houseids, $type, $expiredtype){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid IN( ' . pwImplode($houseids) . ') AND datatype = ' . pwEscape($type) . ' AND expiredtype = ' . pwEscape($expiredtype);
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query,'houseid');
	}
	/**
	 * 获取过期的数据
	 */
	function getExpireds() {
		global $timestamp;
		$_sql = 'SELECT * FROM '.$this->_tableName.' WHERE expiredtime<'.$this->_addSlashes($timestamp);
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query,'id');
	}
	/**
	 * 更新一条数据
	 * @param $dataType
	 * @param $expiredType
	 * @param $houseid
	 * @param $expiredTime
	 */
	function updateByHouse($dataType,$expiredType,$houseid,$expiredTime) {
		$dataType = (int) $dataType;
		$expiredType = (int) $expiredType;
		$houseid = (int) $houseid;
		$expiredTime = (int) $expiredTime;
		$_sql = 'UPDATE '.$this->_tableName.' SET expiredtime='.$this->_addSlashes($expiredTime).' WHERE datatype='.$this->_addSlashes($dataType).' AND expiredtype='.$this->_addSlashes($expiredType).' AND houseid='.$this->_addSlashes($houseid);
		$this->_db->update($_sql);
	}
	/**
	 * 删除一个房源相关的数据
	 * @param unknown_type $dataType
	 * @param unknown_type $houseid
	 */
	function deleteHouseExpires($dataType,$expiredType,$houseid) {
		$dataType = (int) $dataType;
		$expiredType  = (int) $expiredType;
		$houseid = (int) $houseid;
		
		$_sql = 'DELETE FROM '.$this->_tableName.' WHERE datatype='.$this->_addSlashes($dataType).' AND expiredtype = '. $this->_addSlashes($expiredType).' AND houseid='.$this->_addSlashes($houseid);
		$this->_db->update($_sql);
	}
	/**
	 * 删除所有房源相关的数据
	 * @param unknown_type $dataType
	 * @param unknown_type $houseid
	 */
	function deleteAllHouseExpires($dataType,$houseid) {
		$dataType = (int) $dataType;
		$houseid = (int) $houseid;
		
		$_sql = 'DELETE FROM '.$this->_tableName.' WHERE datatype='.$this->_addSlashes($dataType).' AND houseid='.$this->_addSlashes($houseid);
		$this->_db->update($_sql);
	}
	/**
	 * 批量删除一部分数据
	 * @param $ids
	 */
	function deletes($ids) {
		if (!S::isArray($ids)) return false;
		$_sql = 'DELETE FROM '.$this->_tableName.' WHERE '.$this->_primaryKey.' IN ('.$this->_getImplodeString($ids).')';
		$this->_db->update($_sql);
	}
	
	function insert($data) {
		if (!$this->_check() || !$data) return false;
		$_sql = 'REPLACE INTO ' . $this->_tableName .' SET '. S::sqlSingle($data);
		$this->_db->update($_sql);
		return $this->_db->insert_id();
	}
	
	function getStruct() {
		return array('datatype','expiredtype','houseid','expiredtime');
	}
}